package com.palm.vue;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.zip.GZIPOutputStream;

public class VueResourceLoader {
    public static Function<String, InputStream> resourceLoader= path->loadStream("/static"+path);
    static Map<String, VueResources> templates = new HashMap<>();//放入的地方已经添加锁了，所以不用多线程的HashMap
    static Object lock = new Object();
    static String path="/vue2.js";
    public static String vueVersion;
    public static void setVueVersion(String version,boolean isMin){
        path="/script/vue"+version+(isMin?".min":"")+".js";
        vueVersion=version;
    }
    public static VueResources get(String path) {
        VueResources data = templates.get(path);
        if(data==null) {
            synchronized (lock) {//同步获取,所有的资源共用一个锁，所以尽量减少锁内的消耗
                data = templates.get(path);
                if (data == null) {
                    data = new VueResources();
                    templates.put(path, data);
                } else {
                    return data;
                }
            }
        }else{
            if(data.status){
                return data;
            }
        }
        synchronized (data) {
            if (data.status) {
                return data;
            }
            data.status = true;

            String jsPath=path.replaceFirst("\\.vue",".js");
            InputStream stream = resourceLoader.apply(jsPath);

            if (stream == null) {
//                String vjsPath=path.replaceFirst("\\.vue",".vjs");
//                //return data.data;//return null
//                stream = resourceLoader.apply(vjsPath);
//                if(stream==null){
//                    vjsPath=path.replaceFirst("\\.vue",".js");
//                    stream = resourceLoader.apply(vjsPath);
//                }
//                if(stream==null){
//                    return data;
//                }else{
//                    data.isJs=true;
//                }
                 stream = resourceLoader.apply(path);
                if(stream==null){
                    return data;
                }
            }else{
                data.isJs=true;
            }
            data.data = gzip(loadText(stream));
            data.exists =true;
            return data;
        }
    }
    public static InputStream loadStream(String path){
        return VueResourceLoader.class.getResourceAsStream(path);
    }

    public static String loadText(InputStream is){
        StringBuilder sb = new StringBuilder();
        try(Reader reader = new InputStreamReader(is,"utf-8")) {
            BufferedReader br = new BufferedReader(reader, 512);
            int value;
            while ((value = br.read()) != -1) {
                sb.append((char) value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    private static byte[] gzip(String text) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {
                gzip.write(text.getBytes("UTF-8"));
            }
            return out.toByteArray();
        } catch (IOException e) {
            System.err.println("压缩失败:" + e);
        }
        return null;
    }
    private static byte[] vueData;
    public static byte[] getVue(){
        if(vueData==null){
            InputStream in=loadStream(path);
            vueData=gzip(loadText(in));
        }
        return vueData;
    }
}
